// 状态，类型
var activeStatus = 1,
	activeType = 0;
var roleType = null;
var codeVal = "";

var unitId = null;
//分页初始化
var pageNum = 1;
var pageSize = 10;
var total;
var typeArr = [];
var typeBtn = [];

mui.init({
	pullRefresh: {
		container: '#pullrefresh',
		down: { //下拉刷新
			callback: pulldownRefresh,
			style: mui.os.android ? "circle" : "default"
		},
		up: { //上拉加载
			contentinit: '',
			contentrefresh: '正在加载...',
			contentnomore: '没有更多了',
			callback: pullupRefresh,
		}
	}
});

mui.plusReady(function() {
	roleType = localStorage.getItem("roleType");
	unitId = localStorage.getItem("unitId");
	loadData(activeStatus,activeType)
	//点击进入详情
	mui("#list_wrap").on('tap', 'li', function() {
		var id = this.getAttribute("data-id");
		openNew("alarmDetail.html", {
			alarmId: id
		});
	})
	
	//点击tab重新加载数据
	mui(".mui-bar-tab").on('tap', '.mui-tab-item', function() {
		var type = this.getAttribute("data-type");
		activeStatus = type;
		document.getElementById('list_wrap').innerHTML = '';
		loadData(activeStatus, activeType)
	})

	// request("/app/alarm/code/list/1", "GET", {

	// }, function(res) {
	// 	if(res.code==0){
	// 		typeArr = res.result;
	// 		for(var i=0,len=res.result.length;i<len;i++){
	// 			typeBtn.push({
	// 				title: res.result[i].codeName
	// 			})
	// 		}
	// 	}
	// })
	
	//右上角跳转至设备状态
	/*document.getElementById('jumpToEquipment').addEventListener('tap',function(){
		openNew("equipment.html");
	})*/
	
	//左上角选择状态
	document.getElementById("selectType").addEventListener('tap', function() {
		typeBtn = [];
		var btnArray = [{
			title: "全部"
		}, {
			title: "远程监控系统"
		}, {
			title: '智慧用水系统'
		}, {
			title: '智慧用电系统'
		}, {
			title: '智能预警系统'
		}, {
			title: '燃气监测系统'
		}, {
			title: '智能电桩系统'
		}, {
			title: '智能疏散系统'
		}, {
			title: '视频监控系统'
		}];
		plus.nativeUI.actionSheet({
			title: "选择类型",
			cancel: "取消",
			buttons: btnArray
		}, function(e) {
			var index = e.index;

			if (index > 0) {
				activeType = index - 1;
			}
			request("/app/alarm/code/list/"+activeType, "GET", {

			}, function(res) {
				if(res.code==0){
					typeArr = res.result;
					for(var i=0,len=res.result.length;i<len;i++){
						typeBtn.push({
							title: res.result[i].codeName
						})
					}
				}
			})
			if (activeType >= 0) {
				document.getElementById('list_wrap').innerHTML = '';
				loadData(activeStatus, activeType)
			}

			/*switch (index) {
				case 0:
					activeType = index;
					loadData(activeStatus,activeType)
					break;
				case 1:
					text += "拍照或录像";
					break;
				case 2:
					text += "选取现有的";
					break;
			}*/
		});
	});

	document.getElementById("newType").addEventListener('tap', function() {
		var btnArray = typeBtn;
		plus.nativeUI.actionSheet({
			title: "选择类型",
			cancel: "取消",
			buttons: btnArray
		}, function(e) {
			var index = e.index;
			codeVal = typeArr[index-1].codeValue;
			document.getElementById('list_wrap').innerHTML = '';
			loadData(activeStatus, activeType)
		});
	});


})

//下拉刷新具体业务实现
function pulldownRefresh() {
	//重置页码
	pageNum = 1;
	document.getElementById('list_wrap').innerHTML = '';
	loadData(activeStatus, activeType);
}
// 上拉加载具体业务实现
function pullupRefresh() {
	loadData(activeStatus, activeType, true);
}
//一次性拉取数据

function loadData(status, type, isnextpage, isreload) {
	document.getElementById('list_wrap').innerHTML = '';
	if (isnextpage&&(pageNum * pageSize < total)) { //加载下一页
		pageNum++;
	} else if (isreload) { //重新加载当前页
		pageNum = curr_pageno;
	} else if (pageNum == 0) {
		pageNum = 1; //未加载过
	} else {
		pageNum = 1; //默认加载第一页
	}
	var showload = pageNum == 1;
	var isappend = pageNum > 1 ? true : false;
	log(pageNum + "," + total + "," + showload + "," + isappend)
	if (pageNum * pageSize >= total) {
		appPage.endPullRefresh(true);
	}

	request("/app/alarm/list", "GET", {
		status: status,
		type: type,
		roleType: roleType,
		unitId: unitId,
		codeVal: codeVal,
		pageNum: pageNum,
		pageSize: pageSize

	}, function(res) {
		/*if (res.code == 0) {
			render('#list_wrap', 'alarmInfoList', res, true)
		}
		alert(status+","+type)*/
		console.log(JSON.stringify(res))
		pageNum = res.pager.pageNum;
		pageSize = res.pager.pageSize;
		total = res.pager.total;

		var nomore = true;
		if (res.code == 0) {
			render("#list_wrap", "alarmInfoList", res,true, isappend);
			nomore = pageNum * pageSize >= total;
		}
		appPage.endPullRefresh(nomore);
	}, true, function() {
		appPage.endPullRefresh();
		/*var arr = document.getElementsByClassName("nodata");
		/*var arr = document.getElementsByClassName("nodata");
		for (var i = 0; i < arr.length; i++) {
			arr[i].innerText = "暂无数据";
		}*/
		
	});
}
